//
//  QDMetroErrorHeaderView.m
//  QDMetroClient
//
//  Created by YueWen on 2018/12/28.
//  Copyright © 2018 YueWen. All rights reserved.
//

#import "QDMetroErrorHeaderView.h"

/// 根据不同的type显示不同的error信息
FOUNDATION_STATIC_INLINE NSString *QDMetroErrorHeaderViewTitle(QDMetroHeaderErrorType type){
    switch (type) {
        case QDMetroHeaderErrorTypeOutBluetooth: return @"未开启蓝牙";
        case QDMetroHeaderErrorTypeInBlacklist: return @"您存在未扣费订单";
        case QDMetroHeaderErrorTypeNetFail: return @"网络请求失败";
        case QDMetroHeaderErrorTypeOutPayType: return @"未绑定支付方式";
        case QDMetroHeaderErrorTypeLoginStatus: return @"未登录账号";
        case QDMetroHeaderErrorTypeOutDefaultChannel: return @"没有绑定默认支付方式";
        case QDMetroHeaderErrorTypeNFC: return @"当前系统不支持NFC";
        case QDMetroHeaderErrorTypeNone: return @"正在尝试连接";
        default: return @"";
    }
}


/// 根据不同的type显示不同的解决办法
FOUNDATION_STATIC_INLINE NSString *QDMetroErrorHeaderViewSubtitle(QDMetroHeaderErrorType type){
    switch (type) {
        case QDMetroHeaderErrorTypeOutBluetooth: return @"请开启蓝牙，以便正常使用";
        case QDMetroHeaderErrorTypeInBlacklist: return @"别着急，联系客服问问看";
        case QDMetroHeaderErrorTypeNetFail: return @"获取二维码失败，请刷新页面";
        case QDMetroHeaderErrorTypeOutPayType: return @"请绑定一个支付方式，用于乘车码扣款";
        case QDMetroHeaderErrorTypeLoginStatus: return @"请先登录您的账号";
        case QDMetroHeaderErrorTypeOutDefaultChannel: return @"请绑定一个默认支付方式";
        case QDMetroHeaderErrorTypeNFC: return @"当前系统不支持NFC乘车，请切换二维码乘车";
        case QDMetroHeaderErrorTypeNone: return @"正在请求二维码信息";
        default: return @"";
    }
}



/// 根据不同的type显示不同的按钮功能
FOUNDATION_STATIC_INLINE NSString *QDMetroErrorHeaderViewButtonTitle(QDMetroHeaderErrorType type){
    switch (type) {
        case QDMetroHeaderErrorTypeOutBluetooth: return @"开启蓝牙";
        case QDMetroHeaderErrorTypeInBlacklist: return @"联系客服";
        case QDMetroHeaderErrorTypeNetFail: return @"刷新";
        case QDMetroHeaderErrorTypeOutPayType: return @"去绑定";
        case QDMetroHeaderErrorTypeLoginStatus: return @"立即登录";
        case QDMetroHeaderErrorTypeOutDefaultChannel: return @"立即设置";
        case QDMetroHeaderErrorTypeNFC: return @"切换二维码";
        case QDMetroHeaderErrorTypeNone: return @"请稍等";
        default: return @"";
    }
}



/// 根据不同的type显示不同的图片名称
FOUNDATION_STATIC_INLINE NSString *QDMetroErrorHeaderViewImageName(QDMetroHeaderErrorType type){
    switch (type) {
        case QDMetroHeaderErrorTypeOutBluetooth: return @"mc_error_bluetooth";
        case QDMetroHeaderErrorTypeInBlacklist: return @"mc_error_black";
        case QDMetroHeaderErrorTypeNetFail: return @"mc_error_net";
        case QDMetroHeaderErrorTypeOutPayType: return @"mc_error_outPayType";
        case QDMetroHeaderErrorTypeLoginStatus: return @"mc_error_login";
        case QDMetroHeaderErrorTypeOutDefaultChannel: return @"mc_error_outPayType";
        case QDMetroHeaderErrorTypeNone: return @"mc_error_net";
        case QDMetroHeaderErrorTypeNFC: return @"mc_error_nfc";
        default: return @"";
    }
}

@interface QDMetroErrorHeaderView ()

/// 记录当前的错误类型
@property (nonatomic, assign, readwrite)QDMetroHeaderErrorType errorType;

@end


@implementation QDMetroErrorHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.imageView = ({
            
            UIImageView *imageView = [UIImageView new];
            imageView.backgroundColor = UIColor.whiteColor;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            
            imageView;
        });
        
        
        self.titleLabel = ({
            
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor whiteColor];
            label.text = @"未开启蓝牙";
            label.font = RITLMediumFont(16);
            label.textColor = QDSColorDefaultBlack;
            
            label;
        });
        
        
        self.subtitleLabel = ({
            
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor whiteColor];
            label.text = @"请开启蓝牙，以便正常使用";
            label.font = RITLRegularFont(14);
            label.textColor = QDSColorDefaultGray2;
            
            label;
        });
        
        self.handerButton = ({
            
            UIButton *view = [UIButton new];
            view.clipsToBounds = true;
            view.layer.cornerRadius = 20;
            view.adjustsImageWhenHighlighted = false;
            view.titleLabel.font = RITLSemiboldFont(14);
            [view setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            
            view;
        });
        

        CAGradientLayer *buttonLayer = [CAGradientLayer ritl_GradientLayer:CGRectMake(0, 0, 100, 40) FirstColor:RITLColorFromIntRBG(115, 222, 133) SecondColor:RITLColorFromIntRBG(105, 201, 121)];
        [self.handerButton.layer insertSublayer:buttonLayer atIndex:0];
        
        [self.contentView addSubview:self.imageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.subtitleLabel];
        [self.contentView addSubview:self.handerButton];
        
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(140);
            make.centerX.offset(0);
            make.top.offset(36 + QDErrorAddtionTopDistance());
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).offset(8);
            make.centerX.offset(0);
            make.height.mas_lessThanOrEqualTo(22);
        }];
        
        [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(4);
            make.centerX.offset(0);
            make.height.mas_lessThanOrEqualTo(20);
        }];
        
        [self.handerButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(100);
            make.centerX.offset(0);
            make.bottom.inset(28 + QDErrorAddtionBottomDistance());
        }];
        
        //强制刷新一次
        _errorType = QDMetroHeaderErrorTypeOutPayType;
        [self updateHeaderViewWithType:QDMetroHeaderErrorTypeNetFail];
        
        //追加响应
        __weak typeof(self) weakSelf = self;
        [self.handerButton controlEvents:UIControlEventTouchUpInside handle:^(__kindof UIControl * _Nonnull sender) {
            [weakSelf.delegate qdMetroErrorHeaderView:weakSelf handerButtonDidTap:weakSelf.errorType];
        }];
    }
    
    return self;
}


- (void)updateHeaderViewWithType:(QDMetroHeaderErrorType)errorType {
    if (self.errorType == errorType) { return; }
    
    // update message
    self.errorType = errorType;
    self.titleLabel.text = QDMetroErrorHeaderViewTitle(errorType);
    self.subtitleLabel.text = QDMetroErrorHeaderViewSubtitle(errorType);
    self.imageView.image = QDMetroErrorHeaderViewImageName(errorType).ritl_image;
    [self.handerButton setTitle:QDMetroErrorHeaderViewButtonTitle(errorType) forState:UIControlStateNormal];
    
}

@end
